package ar.edu.utn.frsf.tpae.a2014.g01.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import ar.edu.utn.frsf.tpae.a2014.g01.dao.UsuarioDAO;
import ar.edu.utn.frsf.tpae.a2014.g01.entidad.Transportista;
import ar.edu.utn.frsf.tpae.a2014.g01.entidad.Usuario;

public class UsuarioHibernateDAO implements UsuarioDAO {

	private static UsuarioHibernateDAO instance = new UsuarioHibernateDAO();
	private SessionFactory sessionFactory = new AnnotationConfiguration()
      .configure().buildSessionFactory();

	public static UsuarioHibernateDAO getInstance(){
		return instance;
	}

	private UsuarioHibernateDAO(){
		
	}
	

	@Override
	public boolean validarUsuario(String usuario, String password) {

		Session session = sessionFactory.openSession();
	    session.beginTransaction();
	    List <Usuario> list = session.createQuery("FROM Usuario WHERE usuario='"+usuario+"' and password='"+password+"'").list();
	    session.getTransaction().commit();
	    session.close();
	    return list.size()==1;

	}

	@Override
	public List<Transportista> buscarTransportistas() {

		Session session = sessionFactory.openSession();
	    session.beginTransaction();
	    List list = session.createQuery("FROM Transportista").list();
	    session.getTransaction().commit();
	    session.close();
	    return list;
	}

}
